let MS = require("./ms.js")
const request = require("./request.js")
const Error = require('./error')
const {
    isString,
    isUndefined,
    isNumber,
    isBoolean,
    isArray,
    isObject
} = require('./dataType')

const ship = class ship {
    constructor() {}

    get(shipId) {
        let params = {
            shipId: shipId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.GET.url,
                MS._config.parameters.SHIP.GET.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    findShipByUserId(userId) {
        let params = {
            userId: userId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.FIND_SHIP_BY_USER_ID.url,
                MS._config.parameters.SHIP.FIND_SHIP_BY_USER_ID.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    findEnableShip() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.FIND_ENABLE_SHIP.url,
                MS._config.parameters.SHIP.FIND_ENABLE_SHIP.method, null).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    booking(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.shipId) && isString(options.shipId)) {
                params.shipId = options.shipId;
            }
            if(!isUndefined(options.memberId) && isString(options.memberId)) {
                params.memberId = options.memberId;
            }
            if(!isUndefined(options.startTimeStr) && isString(options.startTimeStr)) {
                params.startTimeStr = options.startTimeStr;
            }
            if(!isUndefined(options.endTimeStr) && isString(options.endTimeStr)) {
                params.endTimeStr = options.endTimeStr;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.BOOKING.url,
                MS._config.parameters.SHIP.BOOKING.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    cancel(bookingShipsId) {
        let params = {
            id: bookingShipsId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.CANCEL.url,
                MS._config.parameters.SHIP.CANCEL.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    conflict(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.shipId) && isString(options.shipId)) {
                params.shipId = options.shipId;
            }
            if(!isUndefined(options.startTimeStr) && isString(options.startTimeStr)) {
                params.startTimeStr = options.startTimeStr;
            }
            if(!isUndefined(options.endTimeStr) && isString(options.endTimeStr)) {
                params.endTimeStr = options.endTimeStr;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.CONFLICT_DATA.url,
                MS._config.parameters.SHIP.CONFLICT_DATA.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    costCalculation(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.shipId) && isString(options.shipId)) {
                params.shipId = options.shipId;
            }
            if(!isUndefined(options.memberId) && isString(options.memberId)) {
                params.memberId = options.memberId;
            }
            if(!isUndefined(options.startTimeStr) && isString(options.startTimeStr)) {
                params.startTimeStr = options.startTimeStr;
            }
            if(!isUndefined(options.endTimeStr) && isString(options.endTimeStr)) {
                params.endTimeStr = options.endTimeStr;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.COST_CALCULATION.url,
                MS._config.parameters.SHIP.COST_CALCULATION.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    divide(options) {
        let params = {}
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = params.userId;
        }
        if(!isUndefined(options.shipId) && isString(options.shipId)) {
            params.deviceId = options.shipId;
        }
        if(!isUndefined(options.mine) && isBoolean(options.mine)) {
            params.mine = options.mine;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.DIVIDE_USER.url,
                MS._config.parameters.SHIP.DIVIDE_USER.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    detach(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.DETACH_USER.url,
                MS._config.parameters.SHIP.DETACH_USER.method, params).then(res =>{
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(id) {
        let params = {
            shipId: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.REMOVE_DEVICE.url,
                MS._config.parameters.SHIP.REMOVE_DEVICE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(options) {
        let params = {};
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.userId) && isString(options.userId)) {
                params.userId = options.userId;
            }
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.shipLoad) && isNumber(options.shipLoad)) {
                params.shipLoad = options.shipLoad;
            }
            if(!isUndefined(options.maxSpeed) && isNumber(options.maxSpeed)) {
                params.maxSpeed = options.maxSpeed;
            }
            if(!isUndefined(options.length) && isNumber(options.length)) {
                params.length = options.length;
            }
            if(!isUndefined(options.wide) && isNumber(options.wide)) {
                params.wide = options.wide;
            }
            if(!isUndefined(options.draught) && isNumber(options.draught)) {
                params.draught = options.draught;
            }
            if(!isUndefined(options.brand) && isString(options.brand)) {
                params.brand = options.brand;
            }
            if(!isUndefined(options.picture) && isString(options.picture)) {
                params.picture = options.picture;
            }
            if(!isUndefined(options.synopsis) && isString(options.synopsis)) {
                params.synopsis = options.synopsis;
            }
            if(!isUndefined(options.category) && isNumber(options.category)) {
                params.category = options.category;
            }
            if(!isUndefined(options.isVideo) && isBoolean(options.isVideo)) {
                params.isVideo = options.isVideo;
            }
            if(!isUndefined(options.isLease) && isBoolean(options.isLease)) {
                params.isLease = options.isLease;
            }
            if(!isUndefined(options.status) && isBoolean(options.status)) {
                params.status = options.status;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.SAVE.url,
                MS._config.parameters.SHIP.SAVE.method, params).then(res =>{
                resolve(res.data)
            }).catch(err =>{
                reject(err)
            })
        })
    }

    loadShipCategory() {
        let params = {}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHIP.LOAD_SHIP_CATEGORY.url,
                MS._config.parameters.SHIP.LOAD_SHIP_CATEGORY.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = ship